package com.ruoyi.repn.controller.front;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.repn.domain.RepOrder;
import com.ruoyi.repn.domain.dto.RepOrderDto;
import com.ruoyi.repn.domain.params.WXPrePayParams;
import com.ruoyi.repn.service.IRepOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author cpf
 * @date 2024/3/4 11:14
 * @description
 */

@RestController
@RequestMapping("/front/order")
public class FrontOrderController extends BaseController
{
    @Autowired
    private IRepOrderService repOrderService;





    /**
     * 查询年报订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RepOrder repOrder)
    {
        startPage();
        List<RepOrderDto> list = repOrderService.selectRepOrderList(repOrder);
        return getDataTable(list);
    }


    /**
     * 获取年报订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(repOrderService.selectRepOrderById(id));
    }


    /**
     * 新增年报订单
     */
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody RepOrder repOrder)
    {
        return repOrderService.insertRepOrder(repOrder);
    }


    /**
     * 修改年报订单
     */
    @RepeatSubmit
    @PutMapping
    public AjaxResult edit(@RequestBody RepOrder repOrder)
    {
        return toAjax(repOrderService.updateRepOrder(repOrder));
    }



    /**
     * 预支付
     * @param wxPrePayParams 预支付请求参数
     * @return  WxPayRespVO
     */
    @RepeatSubmit  //防止重复提交
    @PostMapping("/wx/pre/pay")
    public AjaxResult prePay(@RequestBody @Validated WXPrePayParams wxPrePayParams){
        return AjaxResult.success(repOrderService.prePay(wxPrePayParams));
    }


    /**
     * 根据商户订单号查询订单是否支付成功
     * @param orderNo 商户订单号
     */
    @GetMapping("/isPay/{orderNo}")
    public AjaxResult isPay(@PathVariable String orderNo){
        return repOrderService.isPay(orderNo);
    }


    /**
     *  支付回调
     *  回调接口需要暴露到公网上，且要放开token验证
     */
    @PostMapping("/payNotify")
    public ResponseEntity payNotify(HttpServletRequest request){
        return repOrderService.payNotify(request);
    }

}
